﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using DaInterFace;
using DaInterFace.SYN;
using DaInterFace.SYN.E01;
using System.Reflection;

namespace E02
{
    /// <summary>
    /// 同步采购计量单
    /// </summary>
    public class A06 : ISYNChildGet
    {
        public void Code(ISYN handler, ref CodeResult result, SYNRight user, SerialType st, String baseNo, String billId)
        {
            try
            {
                result.Code = "E02.A06";
                List<E02A06P> ht = new List<E02A06P>();
                SqlParameter[] sps = new SqlParameter[1];
                sps[0] = new SqlParameter("@FID", SqlDbType.NVarChar, 50);
                sps[0].Value = billId;
                string sql = " SELECT a.FID BillId,a.FBillNo BillNo,a.FComboBox BillStatus,FComboBox2 BillType,a.FNOTE BillNote," +
                             "a.FText CarNo,FText8 CardNo,a.FDecimal4 YNum,case when FTime = '' then '1990-01-01 00:00:00' when FTime IS null then '1990-01-01 00:00:00' else FTime end KPTime,a.FDecimal InMeasure," +
                             "case when FTime1 = '' then '1990-01-01 00:00:00' when FTime1 IS null then '1990-01-01 00:00:00' else FTime1 end InTime,a.FDecimal1 OutMeasure,case when FTime2 = '' then '1990-01-01 00:00:00' when FTime2 IS null then '1990-01-01 00:00:00' else FTime2 end OutTime,a.FDecimal2 FactWeight,FDecimal17 ManualButton," +
                             "FComboBox4 ButtonWay,a.FDecimal3 InNum,FText10 ButtonReason," +
                             "FText1 BillingStaff,FText2 InWeight,FText3 OutWeight," +
                             "FText4 QCheckNo,FText9 AcceptMan,FDecimal21 ProfitLoss,d.FTEXT BulkMaterial,b.F_102 Material,c.F_102 Supplier," +
                             "FCheckBox4 CancelFlag,FDecimal22 HistoryWeight,FText12 CarIdentity,FComboBox3 UnloadingWay," +
                             "FText13 AcceptPlace,FNOTE3 PricePolicy,FText5 UpdateFlag,isnull(FBASE6,0) NeedCheck,(select FText5 from CardServer.dbo.KEW_DataBaseEntry where FNumber = " + baseNo + " ) FiUnitID  FROM KEW_PurchInfo a " +
                             "left join KEW_PURCHITEMENTRY d on d.FID = a.FBASE left join t_ICItem b on d.FBASE = b.FItemID " +
                             "left join t_Supplier c on a.FBASE1 = c.FItemID " +
                             "left join KEW_PURCHINFOENTRY e on e.fid = a.fid " +
                             "left join KEW_PurchOrderQty f on f.fid = e.FID_SRC where a.FID = @FID ";
                sql = sql + " SELECT a.FEntryID BillId,FID_SRC SrcID,FClassID_SRC SrcBillType,FBillNo_SRC OrderNo,FDecimal11 Num,FDecimal5 LeftNum,b.F_102 Supplier," +
                            "FDecimal7 MaterialPrice,FDecimal8 MaterialTaxRate,FDecimal13 TaxTranPrice,FDecimal12 TranTaxRate,FDecimal14 OtherPrice,FDecimal15 TotalMoney,FDecimal9 InNum," +
                            "FDecimal10 InMoney,FDecimal20 Balance,FDecimal16 TStore  FROM KEW_PurchInfoEntry a  left join t_Supplier b on a.FBASE4 = b.FItemID WHERE a.FID=@FID";
                            
                Recordset rs = handler.Data.ExeProcedure(baseNo, sql, sps);
                if (rs.Enable)
                {
                    if (rs.Data.Tables[0].Rows.Count == 1)
                    {
                        DataRow row = rs.Data.Tables[0].Rows[0];
                        if (row["Supplier"] == null || row["Supplier"].ToString().Equals(""))
                        {
                            result.Result = EnumResult.None;
                            result.Message = String.Format("采购计量单数据提取错误:{0}", "请先配置基础资料供应商对应关系！编号：" + row["BillNo"].ToString());
                            return;
                        }
                        if (row["Material"] == null || row["Material"].ToString().Equals(""))
                        {
                            result.Result = EnumResult.None;
                            result.Message = String.Format("采购计量单数据提取错误:{0}", "请先配置基础资料物料对应关系！编号：" + row["BillNo"].ToString());
                            return;
                        }
                        if (row["BulkMaterial"] == null || row["BulkMaterial"].ToString().Equals(""))
                        {
                            result.Result = EnumResult.None;
                            result.Message = String.Format("采购计量单数据提取错误:{0}", "请先配置基础资料大宗原料对应关系！编号：" + row["BillNo"].ToString());
                            return;
                        }
                        E02A06P e02A06P = new E02A06P();
                        for (int i = 0; i < rs.Data.Tables[0].Columns.Count; i++)
                        {
                            DataColumn dc = rs.Data.Tables[0].Columns[i];
                            ReflectUtil.setValue(e02A06P, dc.ColumnName, row[dc.ColumnName].ToString());
                        }

                        List<E02A06E> OrderList = new List<E02A06E>();

                        foreach (DataRow entry in rs.Data.Tables[1].Rows)
                        {
                            E02A06E e02A06E = new E02A06E();
                            for (int j = 0; j < rs.Data.Tables[1].Columns.Count; j++)
                            {
                                DataColumn dc = rs.Data.Tables[1].Columns[j];
                                ReflectUtil.setValue(e02A06E, dc.ColumnName, entry[dc.ColumnName].ToString());
                            }
                            OrderList.Add(e02A06E);
                        }
                        e02A06P.strOrderList = handler.Json.ClassToJson(OrderList);
                        ht.Add(e02A06P);
                        result.Content = handler.Json.ClassToJson(ht);
                    }
                    else
                    {
                        result.Result = EnumResult.None;
                    }
                }
                else
                {
                    result.Result = EnumResult.None;
                    result.Message = String.Format("采购计量单数据提取错误:{0}", rs.Message);
                }
            }
            catch (Exception ex)
            {
                result.Result = EnumResult.None;
                result.Message = String.Format("采购计量单数据提取错误:{0}", ex.Message);
            }
        }

    }
    /// <summary>
    /// 采购计量单
    /// </summary>
    public class E02A06P
    {
        /// <summary>
        /// 账套编号
        /// </summary>
        public String BaseNo;
        /// <summary>
        /// 内码
        /// </summary>
        public String ID;
        public String BillId = String.Empty; //单据ID
        public String BillNo = String.Empty; //过磅编号
        public String BillStatus = String.Empty; //称量状态
        public String BillType = String.Empty; //过磅类型
        public String BillNote = String.Empty; //备注
        public String CarNo = String.Empty; //车牌号
        public String Supplier = String.Empty; //供应商
        public String BulkMaterial = String.Empty; //大宗原料
        public String Material = String.Empty; //物料
        public Decimal YNum = 0; //原发数量
        public String KPTime; //开票时间
        public Decimal InMeasure = 0; //进厂计量
        public String InTime; //进厂时间
        public Decimal OutMeasure = 0; //出厂计量
        public String OutTime ; //出厂时间
        public Decimal FactWeight = 0; //实际净重
        public Decimal ManualButton = 0; //手动扣吨
        public String ButtonWay = String.Empty; //方式
        public Decimal InNum = 0; //入库数量
        public String ButtonReason = String.Empty; //扣吨原因
        public String BillingStaff = String.Empty; //开票人员
        public String InWeight = String.Empty; //进厂司磅
        public String OutWeight = String.Empty; //出厂司磅
        public String CardNo = String.Empty; //磁卡
        public String QCheckNo = String.Empty; //质检编号
        public String AcceptMan = String.Empty; //验收员
        public Decimal ProfitLoss = 0; //盈亏
        public Boolean CancelFlag = false; //作废标志
        public Decimal HistoryWeight = 0; //历史皮重
        public String CarIdentity = String.Empty; //车牌识别
        public String UnloadingWay = String.Empty; //卸货方式
        public String AcceptPlace = String.Empty; //收料地点
        public String PricePolicy = String.Empty; //价格政策
        public String UpdateFlag = String.Empty; //更新标志
        public String strOrderList = String.Empty; //订单
        public Int32 SrcID = 0; //源单内码
        public Int32 NeedCheck;

        /// <summary>
        /// EAS财务组织
        /// </summary>
        public String FiUnitID;
    }
    public class E02A06E
    {
        public String BillId = String.Empty; //单据ID
        public String OrderId = String.Empty; //订单ID
        public String OrderNo = String.Empty; //订单编号
        public String Supplier = String.Empty; //供应商
        public Decimal Num = 0; //数量
        public Decimal LeftNum = 0; //剩余数量
        public Decimal MaterialPrice = 0; //原料单价
        public Decimal MaterialTaxRate = 0; //原料税率
        public Decimal TaxTranPrice = 0; //含税运价
        public Decimal TranTaxRate = 0; //运费税率
        public Decimal OtherPrice = 0; //其他单价
        public Decimal TotalMoney = 0; //合计
        public Decimal InNum = 0; //购进数量
        public Decimal InMoney = 0; //购进金额
        public Decimal Balance = 0; //结存
        public Decimal TStore = 0; //暂存
        /// <summary>
        /// 源单内码
        /// </summary>
        public String SrcID;
        /// <summary>
        /// 源单类型
        /// </summary>
        public String SrcBillType;
    }
}
